package com.ruoyi.web.controller.business;

import cn.hutool.core.map.MapUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.TopicalType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.utils.StringUtil;
import com.ruoyi.system.domain.BCase;
import com.ruoyi.system.domain.BTopical;
import com.ruoyi.system.domain.request.TopicalRequest;
import com.ruoyi.system.domain.vo.BCaseVo;
import com.ruoyi.system.domain.vo.BTopicalVo;
import com.ruoyi.system.service.BCaseService;
import com.ruoyi.system.service.BTopicalService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 问答管理
 * @author yangxz
 **/
@RestController
@RequestMapping("/business/topical")
public class TopicalController extends BaseController {

    @Autowired
    BTopicalService topicalService;

    @Autowired
    BCaseService caseService;

    /**
     * 分页获取主题列表
     */
    @PreAuthorize("@ss.hasPermi('dict:topical:list')")
    @GetMapping("/list")
    public TableDataInfo queryList(TopicalRequest request){
        startPage();
        if(StringUtil.isEmpty(request.getType())){
            request.setType(TopicalType.CREATE.getValue());
        }
        List<BTopicalVo> list = topicalService.queryList(request);
        return getDataTable(list);
    }

    /**
     * 创建主题
     * @param request
     * @return
     */
    @PreAuthorize("@ss.hasPermi('dict:topical:add')")
    @Log(title = "创建主题", businessType = BusinessType.INSERT)
    @PostMapping("/create")
    public AjaxResult create(@Validated @RequestBody TopicalRequest request){
        BCase bCase = caseService.getById(request.getCaseId());
        BTopical topical = new BTopical();
        BeanUtils.copyProperties(request,topical);
        topical.setUserId(bCase.getUserId());
        topical.setCreateTime(new Date());
        topical.setType(TopicalType.CREATE.getValue());
        topicalService.save(topical);

        return success();
    }

    /**
     * 更新主题
     * @param request
     * @return
     */
    @PreAuthorize("@ss.hasPermi('dict:topical:update')")
    @Log(title = "更新主题", businessType = BusinessType.INSERT)
    @PostMapping("/update")
    public AjaxResult update(@Validated @RequestBody TopicalRequest request){
        BTopical topical = new BTopical();
        BeanUtils.copyProperties(request,topical);
        topical.setCreateTime(new Date());
        topical.setType(TopicalType.CREATE.getValue());
        topicalService.updateById(topical);

        return success();
    }

    /**
     * 删除主题
     */
    @PreAuthorize("@ss.hasPermi('dict:topical:remove')")
    @Log(title = "删除任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{topicalIds}")
    public AjaxResult remove(@PathVariable Long[] topicalIds)
    {
        return toAjax(topicalService.deleteTopicalByIds(topicalIds));
    }


    /**
     * 分页获取主题回复列表
     */
    @PreAuthorize("@ss.hasPermi('dict:reply:list')")
    @GetMapping("/replyList")
    public TableDataInfo queryReplyList(TopicalRequest request){
        startPage();
        List<BTopicalVo> list = topicalService.queryReplyList(request.getTopicalId());
        return getDataTable(list);
    }

    /**
     * 回复主题
     * @param request
     * @return
     */
    @PreAuthorize("@ss.hasPermi('dict:reply:add')")
    @Log(title = "回复主题", businessType = BusinessType.INSERT)
    @PostMapping("/reply")
    public AjaxResult reply(@Validated @RequestBody TopicalRequest request){
        BTopical topical = topicalService.getById(request.getTopicalId());
        if(ObjectUtils.isEmpty(topical)){
            throw new ServiceException("该主题不存在!");
        }
        BTopical reply = new BTopical();
        BeanUtils.copyProperties(request,reply);
//        reply.setUserId(SecurityUtils.getUserId());
        reply.setCaseId(topical.getCaseId());
        reply.setParentId(topical.getId());
        reply.setCreateTime(new Date());
        reply.setType(TopicalType.REPLY.getValue());
        topicalService.save(reply);

        //更新主题回复数
        topical.setReplyNum(topical.getReplyNum()+1);
        topicalService.updateById(topical);
        Map result = MapUtil.builder().put("id",topical.getId()).put("replyNum",topical.getReplyNum()).build();
        return success(result);
    }

    /**
     * 删除主题
     */
    @PreAuthorize("@ss.hasPermi('dict:reply:remove')")
    @Log(title = "删除主题", businessType = BusinessType.DELETE)
    @DeleteMapping("/reply/{replyIds}")
    public AjaxResult removeReply(@PathVariable Long[] replyIds)
    {
        return toAjax(topicalService.deleteReplyByIds(replyIds));
    }

}
